package com.gelonggld.translator.ui

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.gelonggld.translator.mode.LanItemVO
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import java.util.Timer
import java.util.TimerTask
import javax.swing.JDialog
import javax.swing.WindowConstants

class ContentDialog(val project: Project, val virtualFile: VirtualFile) : JDialog() {
    private val lanItems: MutableState<ArrayList<LanItemVO>?> = mutableStateOf(null)

    init {
        defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        title = "选择语言"
        setSize(800, 600)
        val composePanel = ComposePanel()
        contentPane.add(composePanel)
        composePanel.setContent { lanItem(lanItems) }
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                lanItems.value = arrayListOf(
                    LanItemVO(
                        "aa",
                        hashMapOf(
                            "cn" to "aa",
                            "en" to "aa",
                            "rs" to "aa"
                        ),
                        hashMapOf()
                    ),
                    LanItemVO(
                        "cc",
                        hashMapOf(
                            "cn" to "aa",
                            "en" to "aa"
                        ),
                        hashMapOf(
                            "rs" to "aa"
                        )
                    )
                )
            }
        }, 3000)

    }

    @Preview
    @Composable
    private fun lanItem(lanItems: MutableState<ArrayList<LanItemVO>?>) {
        Scaffold {
            if (lanItems.value == null) {
                Column(
                    modifier = Modifier.fillMaxSize(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text("正在加载")
                    LinearProgressIndicator()
                }
            } else {
                Column(
                    modifier = Modifier.fillMaxSize(),
                ) {
                    Row(
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        tableCell("标识(id)", 1f)
                        tableCell("状态", 1f)
                        tableCell("操作", 1f)
                    }
                    lanItems.value!!.forEach {
                        Row(
                            modifier = Modifier.fillMaxWidth()
                                .drawBehind {
                                    val strokeWidth = 1.dp.value
                                    val y = size.height - strokeWidth/2
                                    drawLine(
                                        color = Color.LightGray,
                                        start = Offset(0f,y),
                                        end = Offset(size.width,y),
                                        strokeWidth = strokeWidth
                                    )
                                },
                        ) {
                            tableCell(it.key, 1f)
                            if (it.notShowIn.isEmpty()) {
                                Image(
                                    painter = painterResource("/drawable/succeed.svg"),
                                    contentDescription = null,
                                    contentScale = ContentScale.Inside,
                                    modifier = Modifier.weight(1f, true)
                                        .size(36.dp, 36.dp)
                                        .padding(2.dp)
                                )
                                Box(
                                    contentAlignment = Alignment.Center,
                                    modifier = Modifier.weight(1f, true)
                                ) {
                                    Button(
                                        onClick = {},
                                    ) {
                                        Text("移动")
                                    }
                                }

                            } else {
                                Image(
                                    painter = painterResource("/drawable/failed.svg"),
                                    contentDescription = null,
                                    contentScale = ContentScale.Inside,
                                    modifier = Modifier.weight(1f, true)
                                        .size(36.dp, 36.dp)
                                        .padding(2.dp)
                                )
                                tableCell("无法操作", weight = 1f)
                            }
                        }
                    }
                }
            }
        }
    }

    @Composable
    private fun RowScope.tableCell(text: String, weight: Float) {
        Box(
            modifier = Modifier.weight(weight, true)
                .align(Alignment.CenterVertically),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = text
            )
        }

    }

}